<div class="layui-fluid" id="VIEW-permission" lay-title="权限管理">

    <div class="layui-card" id="permission_edit" style="display: none;">
        <div class="layui-card-header">编辑</div>
        <div class="layui-card-body layui-row layui-col-space10">
            <div class="layui-card-body" style="padding: 15px;">
                <form class="layui-form" action="" lay-filter="form-group">

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="xing">*</span>权限名称:</label>
                            <div class="layui-input-inline">
                                <input type="text" id="pName" name="pName" placeholder="权限名称" lay-verify="required" autocomplete="off" class="layui-input">
                                <input type="hidden" id="pVal" name="pVal"/>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit="" lay-filter="form-permission-edit">确认修改</button>
                            <button type="reset" class="layui-btn layui-btn-primary">清除</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="layui-card">
        <div class="layui-card-header">
            <i class="layui-icon layui-icon-earth"></i>&nbsp;权限
        </div>
        <div class="layui-card-body">
            <div class="layui-form-item">
                <table class="layui-hide" id = "permission-table" lay-filter="permission-table"></table>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['admin', 'table', 'form', 'dropdown', 'jquery','laydate','dtree','treetable'], function (admin, table, form, dropdown, $) {
        var view = $('#VIEW-list-table');
        var $ = layui.$
            , admin = layui.admin
            , element = layui.element
            , layer = layui.layer
            , dtree = layui.dtree
            , treetable = layui.treetable
            , laydate = layui.laydate
            , form = layui.form
            ,addTree
            ,editTree;;
        form.render();


        //渲染表格
        var renderTable = function(){
            layer.load(2);  //加载层
            treetable.render({
                height: 'full-160',
                id:'permission_Tree',
                treeColIndex: 1,	//树形图标显示在第几列
                treeSpid: '0',		//最上级的父级id
                treeIdName: 'id',	//id字段的名称
                treePidName: 'parentId',	//父级节点字段
                treeDefaultClose: false,	//是否默认折叠
                treeLinkage: true,		//父级展开时是否自动展开所有子级
                elem: '#permission-table',	//表格id
                url: 'api/v1//ucenter/sys-permission/permissionsHtmlTree',
                page: true,
                cols: [[
                    {type: 'numbers', title: '编号'},
                    {field: 'id', title: '虚拟权限id'},
                    {field: 'parentId' , title: '上级id'},
                    {field: 'title' , title: '权限名称'},
                    {templet: complain, title: '操作'}
                ]],
                //数据渲染完的回调
                done: function () {
                    //关闭加载
                    layer.closeAll('loading');
                }
            })
        };
        renderTable();
        function complain(d){//操作中显示的内容
            if(d.id.split(":").length==4){
                return [
                    '<a class="layui-btn layui-btn-xs layui-btn-xs" lay-event="edit"><i class="layui-icon">&#xe83f;</i>编辑</a>',
                ].join('');
            }
            else {
                return [
                    '<a class="layui-btn layui-btn-xs" style="background-color:#b3b3b3;border: 1px solid #e6e6e6;cursor: not-allowed;"><i class="layui-icon">&#xe783;</i>虚拟权限不可修改</a>',
                ].join('');
            }
        }

        table.on('tool(permission-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') {
                layer.open({
                    type: 1
                    ,anim: 5
                    ,shade: 0
                    ,maxmin: false
                    ,area:['600px','500px']
                    ,title:'修改数据'
                    ,content: $('#permission_edit')
                });
                $('#pVal').val(data.id);
                $('#pName').val(data.title);

            }
        });

        /* 监听修改提交 */
        form.on('submit(form-permission-edit)', function (data) {
            admin.put({
                api: "changePermission",
                data:{
                    "pVal": data.field.pVal,
                    "pName":data.field.pName,
                },
                //请求成功
                success:function(res){
                    layer.msg(res.data);
                    layer.close(layer.index);
                    window.location.reload();
                }
            });
            return false;
        });
    })
</script>